package cn.pluss.platform.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.gpoint.rdp.wc.param.service.ParamService;
import com.gpoint.rdp.wc.param.service.ParamQueryService;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.core.util.JacksonUtil;
import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.wc.param.persistence.entity.ParamPo;


import java.util.List;


@Controller
@RequestMapping("/wc/param/param/")
public class ParamController extends GenericController{
	@Resource
	private ParamService paramService;
	@Resource
	private ParamQueryService paramQueryService;
	
	
	 @RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  return mv;
	  
	 }

	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = paramQueryService.query(queryFilter);
			PageList<ParamPo> paramList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<ParamPo> list = ParamPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				paramList = new PageList<ParamPo>(list, pageResult);
			}
			
			return new PageJson(paramList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	

	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		ParamPo param=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = paramQueryService.get(id);
			param = ParamPo.fromJsonString(data);
		}

		ModelAndView dd=getAutoView().addObject("paramDemo", param).addObject("returnUrl", preUrl);
		
		//return getAutoView().addObject("param", param).addObject("returnUrl", preUrl);
		return dd;
		
	}
	
	
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		ParamPo param=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = paramQueryService.get(id);
			param = ParamPo.fromJsonString(data);
		}
		return getAutoView().addObject("paramDemo", param).addObject("returnUrl", preUrl);
	}
	
	/** 
	 * 保存【参数表】信息
	 *
	 * @param request
	 * @param response
	 * @param  param
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String json = RequestUtil.getString(request, "json");
			paramService.save(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存参数表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对参数表操作失败,"+e.getMessage());
			logger.error("对参数表操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【参数表】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "id");
			paramService.deleteByIds(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除参数表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除参数表失败，" + e.getMessage());
			logger.error("删除参数表失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	

}